# load relevant libraries 
library(ggplot2)
library(socviz)
library(ggthemes)
library(ggrepel)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.2.1     ✔ stringr 1.5.0
## ✔ readr   2.1.3     ✔ forcats 0.5.2
## ✔ purrr   0.3.5     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
election |> select(state, total_vote, 
                   r_points, pct_trump, party, census) |>
sample_n(5)
## # A tibble: 5 × 6
##   state     total_vote r_points pct_trump party      census   
##   <chr>          <dbl>    <dbl>     <dbl> <chr>      <chr>    
## 1 Maine         747927   -2.96       44.9 Democratic Northeast
## 2 Wisconsin    2976150    0.770      47.2 Republican Midwest  
## 3 Virginia     3982752   -5.32       44.4 Democratic South    
## 4 Nevada       1125385   -2.42       45.5 Democratic West     
## 5 Vermont       315067  -26.4        30.3 Democratic Northeast
# create a colour palette
party_colors <- c("#2E74C0", "#CB454A")

# establish ggplot for election data in DC state
p0 <- ggplot(data = subset(election, st %nin% "DC"), mapping = aes(x = r_points, y = reorder(state, r_points), color = party))

# add our data points and an intercept line
p1 <- p0 + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2)

# add our colour palette to data points
p2 <- p1 + scale_color_manual(values = party_colors)

# fix x-axis
p3 <- p2 + scale_x_continuous(breaks = c( 30, 20, 10, 0, 10, 20, 30, 40), labels = c("30 n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)"))

# use facet wrap to split data 
p3 + facet_wrap(~ census, ncol=1, scales="free_y") + 
  guides(color=FALSE) + 
  labs(x = "Point Margin", y = "") +
  theme(axis.text=element_text(size=8))
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.

# install and preview maps
#install.packages("maps")
library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
us_states <- map_data("state")
head(us_states)
##        long      lat group order  region subregion
## 1 -87.46201 30.38968     1     1 alabama      <NA>
## 2 -87.48493 30.37249     1     2 alabama      <NA>
## 3 -87.52503 30.37249     1     3 alabama      <NA>
## 4 -87.53076 30.33239     1     4 alabama      <NA>
## 5 -87.57087 30.32665     1     5 alabama      <NA>
## 6 -87.58806 30.32665     1     6 alabama      <NA>
# make a map
p <- ggplot(data = us_states, mapping = aes(x = long, y = lat, group = group))

p + geom_polygon(fill = "white", color = "black")

# add colour
p <- ggplot(data = us_states, 
            mapping = aes(x = long, y = lat, group = group, fill = region))
p + geom_polygon(color = 'gray90', linewidth = 0.1) + guides(fill = FALSE)

#changing map projection
p + geom_polygon(color = "gray90", size = 0.1) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)+ 
  guides(fill = FALSE)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.

# adding data to our map
election$region <- tolower(election$state)
us_states_elec <- left_join(us_states, election, by = 'region')

p0 <- ggplot(data = us_states_elec, mapping = aes(x = long, y = lat, group = group, fill = party))

p1 <- p0 + geom_polygon(color = "gray90", size = 0.1) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)

p2 <- p1 + scale_fill_manual(values = party_colors) +
  labs(title = "Election Results 2016", fill = NULL)

p2 + theme_map()

# explore county datasets from socviz
county_map |> sample_n(5)
##        long         lat  order  hole piece            group    id
## 1 -498204.1 -1563068.22 160347 FALSE     1 0500000US48229.1 48229
## 2 1990349.1  -517997.71 171427 FALSE     1 0500000US51057.1 51057
## 3 1922656.2  -250105.46 140163 FALSE     1 0500000US42041.1 42041
## 4 2172616.3   211242.44 169482 FALSE     1 0500000US50027.1 50027
## 5  565841.8    46453.54  95242 FALSE     1 0500000US27163.1 27163
county_data |> select(id, name, state, pop_dens) |> sample_n(5)
##      id             name state      pop_dens
## 1 13135  Gwinnett County    GA [ 1000, 5000)
## 2 19055  Delaware County    IA [   10,   50)
## 3 48271    Kinney County    TX [    0,   10)
## 4 06111   Ventura County    CA [  100,  500)
## 5 40089 McCurtain County    OK [   10,   50)
# merge county map with county data
county_full <- left_join(county_map, county_data, by = "id")

# plotting population density

p <- ggplot(data = county_full, mapping = aes(x = long, y = lat, fill = pop_dens, group = group))

p1 <- p + geom_polygon(color = "gray90", size = 0.05) +
  coord_equal()

p2 <- p1 + scale_fill_brewer(palette="Blues", labels = c("0-10",
"10-50", "50-100", "100-500", "500-1,000", "1,000-5,000", ">5,000"))

p2 + labs(fill = "Population per nsquare mile") +
  theme_map() +
  guides(fill = guide_legend(nrow = 1)) +
  theme(legend.position = "bottom")

# flow charts in R
#install.packages("DiagrammeR")
library(DiagrammeR)

# make basic flow chart
p0 <- grViz(diagram = "digraph flowchart {
  node [fontname = arial, shape = oval] # 'NODE' defines our nodes' appearance
  tab1 [label = '@@1'] # define how many nodes
  tab2 [label = '@@2']
  tab3 [label = '@@3']
  tab1 -> tab2 -> tab3; #define connections between nodes
}

  [1]: 'Artefact collection in field' #define text of nodes
  [2]: 'Preliminary dating of artefacts (visual)'
  [3]: 'Artefacts sent to lab for dating'
  ")

p0